objArgs = WScript.Arguments;
num = objArgs.length;

if(num != 6)
{
	WScript.Echo("only support PostCommit type hook!!!");
	WScript.Quit(1);
}

var serverPath = GetServerWorkPath()
var localPath = GetWorkPath()

localPath = localPath.toString().replace("\\", "/")

var paths = ReadPaths(objArgs(0));
var msg = "("
var szLog = ""

for(i =0; i < paths.length; i++)
{
	if(i == paths.length - 1)
	{
		msg = msg + "echo " + paths[i].toString().replace(localPath, serverPath) +"@"+ objArgs(3) + ")";
		szLog = szLog + paths[i].toString().replace(localPath, serverPath) +"@"+ objArgs(3)+ "\r\n";
	}
	else
	{
		msg = msg + "echo " + paths[i].toString().replace(localPath, serverPath) +"@"+ objArgs(3) + "&";
		szLog = szLog + paths[i].toString().replace(localPath, serverPath) +"@"+ objArgs(3) + "\r\n";
	}
}

Copy2ClipBoard(msg)

var commitLog = ReadLogs(objArgs(2))
CreateLogInfo(commitLog, szLog)

WScript.Quit(0);

//读取配置
function GetServerWorkPath()
{
	var path = GetScriptPath() + "\\config.txt";
	var fs = new ActiveXObject("Scripting.FileSystemObject");
	var workPath = ""
	if(fs.FileExists(path))
	{
		var a = fs.OpenTextFile(path, 1, false);
		workPath = a.ReadLine()
		a.Close();
	}
	return workPath;
}

function GetWorkPath()
{
	var scriptPath = GetScriptPath()
	var index = scriptPath.toString().lastIndexOf("\\");
	return scriptPath.toString().substr(0, index);
}

function GetScriptPath()
{
	var fs = new ActiveXObject("Scripting.FileSystemObject");
	return fs.GetParentFolderName(WScript.ScriptFullName)
}

//获取操作文件列表
function ReadPaths(path)
{
	var retPaths = new Array();
	var fs = new ActiveXObject("Scripting.FileSystemObject");
	if(fs.FileExists(path))
	{
		var a = fs.OpenTextFile(path, 1, false);
		var i = 0;
		while(!a.AtEndOfStream)
		{
			var line = a.ReadLine();
			retPaths[i] = line;
			i= i +1;
		}
		a.Close();
	}
	return retPaths;
}

function ReadLogs(path)
{
	var log = ""
	var fs = new ActiveXObject("Scripting.FileSystemObject");
	if(fs.FileExists(path))
	{
		var a = fs.OpenTextFile(path, 1, false);
		while(!a.AtEndOfStream)
		{
			var line = a.ReadLine();
			log = log + line;
		}
		a.Close();
	}
	return log
}

function Copy2ClipBoard(msg)
{
	WSHShell = WScript.CreateObject("WScript.Shell");
	WSHShell.Run("cmd.exe /q /c "+msg+"|clip", 0, true);
	
	WScript.Echo("Commit Content had copy to ClipBoard!!!!");
}

function GetLogFileName()
{
	var tmpDate = new Date();
	return GetScriptPath() + "//" + tmpDate.getFullYear().toString() + (tmpDate.getMonth() + 1).toString() + tmpDate.getDate().toString() +".txt";
}

function CreateLogInfo(commitLog, msg)
{
	var file;
	var fs = new ActiveXObject("Scripting.FileSystemObject");
	if(fs.FileExists(GetLogFileName()))
	{
		file = fs.OpenTextFile(GetLogFileName(), 8, false, false);
	}
	else
	{
		file = fs.CreateTextFile(GetLogFileName(), true, false);
	}
	
	var tmpDate = new Date();
	file.WriteLine("===========================================");
	file.WriteLine("CommitDate:" + tmpDate.getFullYear().toString() +"-"+ (tmpDate.getMonth() + 1).toString() +"-"+ tmpDate.getDate().toString() +"-"+ tmpDate.getMinutes().toString());
	file.WriteLine("CommitLog:");
	file.Write(commitLog);
	file.WriteLine("\r\n");
	file.Write(msg);
	file.WriteLine("\r\n");
	file.WriteLine("===========================================");
	file.Close();
}
